﻿using System;
using System.ComponentModel.DataAnnotations;
using Quintsys.Data.Model.Enums;

namespace Quintsys.Data.Model
{
    public class UsTelephoneNumber
    {
        public UsTelephoneNumber()
        {
        }

        public UsTelephoneNumber(string phoneNumber)
            : this(phoneNumber, PhoneType.Unknown)
        {
        }

        public UsTelephoneNumber(string phoneNumber, PhoneType phoneType)
        {
            if (phoneNumber.Length != 10)
                throw new ArgumentException("Invalid telephone number.");

            AreaCode = Convert.ToInt32(phoneNumber.Substring(0, 3));
            ExchangeCode = Convert.ToInt32(phoneNumber.Substring(3, 3));
            SubscriberNumber = phoneNumber.Substring(6);
            PhoneType = phoneType;
        }

        [Required]
        public PhoneType PhoneType { get; set; }

        [Required]
        [Range(200, 999, ErrorMessage = "Invalid telephone number.")]
        public int? AreaCode { get; set; }

        [Required]
        [Range(200, 999, ErrorMessage = "Invalid telephone number.")]
        public int? ExchangeCode { get; set; }

        [Required]
        [StringLength(maximumLength: 4, MinimumLength = 4, ErrorMessage = "Invalid telephone number.")]
        public string SubscriberNumber { get; set; }
    }
}